package com.dqp.cslggroup.UI;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.dqp.cslggroup.R;

import java.util.Objects;


public class CourseDialog extends Dialog {

    private CourseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {

        private View mLayout;
        private TextView mTitle;
        private TextView mTeacher;
        private TextView mWeeks;
        private TextView mJs;
        private TextView mRoom;
        private TextView bianji_lin, del_lin, xf, kszc, khfs;
        private CourseDialog mDialog;

        public Builder(Context context) {
            mDialog = new CourseDialog(context, R.style.Theme_AppCompat_Dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;
            mLayout = inflater.inflate(R.layout.course_more_dialog, null, false);
            Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            mTitle = mLayout.findViewById(R.id.dialog_title);
            mWeeks = mLayout.findViewById(R.id.dialog_weeks);
            mTeacher = mLayout.findViewById(R.id.dialog_teacher);
            bianji_lin = mLayout.findViewById(R.id.edit);
            del_lin = mLayout.findViewById(R.id.del);
            mJs = mLayout.findViewById(R.id.dialog_js);
            mRoom = mLayout.findViewById(R.id.dialog_room);
            xf = mLayout.findViewById(R.id.dialog_xf);
            kszc = mLayout.findViewById(R.id.dialog_kcxszc);
            khfs = mLayout.findViewById(R.id.dialog_khfsmc);
        }

        /**
         * 设置 Dialog 标题
         */
        public Builder setTitle(@NonNull String title) {
            mTitle.setText(title);
            mTitle.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 设置 Dialog 教师
         */
        public Builder setTeacher(@NonNull String teacher) {
            mTeacher.setText(teacher);
            mTeacher.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 设置 weeks
         */
        public Builder setWeeks(@NonNull String weeks) {
            mWeeks.setText(weeks);
            return this;
        }

        /**
         * 设置 room
         */
        public Builder setRoom(@NonNull String room) {
            mRoom.setText(room);
            return this;
        }

        /**
         * 设置 room
         */
        public Builder setJs(@NonNull String js) {
            mJs.setText(js);
            return this;
        }


        /**
         * 设置 room
         */
        @SuppressLint("SetTextI18n")
        public Builder setxf(@NonNull String x) {
            xf.setText(x + "学分");
            return this;
        }

        /**
         * 设置 room
         */
        public Builder setkhfs(@NonNull String room) {
            khfs.setText(room);
            return this;
        }

        /**
         * 设置 room
         */
        public Builder setkszc(@NonNull String room) {
            kszc.setText(room);
            return this;
        }


        /**
         * 设置按钮文字和监听
         */
        public Builder setEditButton(@NonNull View.OnClickListener listener) {
            bianji_lin.setOnClickListener(v -> {
                if (listener != null)
                    listener.onClick(v);
                mDialog.dismiss();
            });
            return this;
        }

        /**
         * 设置按钮文字和监听
         */
        public Builder setDeleteButton(@NonNull View.OnClickListener listener) {
            del_lin.setOnClickListener(v -> {
                if (listener != null)
                    listener.onClick(v);
            });
            return this;
        }

        /**
         * 设置按钮文字和监听
         */
        public Builder setLongDeleteButton(@NonNull View.OnLongClickListener listener) {
            del_lin.setOnLongClickListener(v -> {
                if (listener != null)
                    listener.onLongClick(v);
                mDialog.dismiss();
                return true;
            });
            return this;
        }

        public CourseDialog create() {
            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);                //用户可以点击后退键关闭 Dialog
            mDialog.setCanceledOnTouchOutside(true);   //用户不可以点击外部来关闭 Dialog
            return mDialog;
        }
    }
}
